package com.ruicar.afs.cloud.demo.demo.workflow;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.security.vo.UserVo;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.workflow.api.dto.GwtCascadeDto;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.request.*;
import com.ruicar.afs.cloud.workflow.gwt.service.WorkflowService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/workflowdemo")
@AllArgsConstructor
public class FlowDemoController {
    private final WorkflowService workflowService;
    private final UidGenerator uidGenerator;

    @GetMapping("/queryAllNode")
    public IResponse queryAllNode() {
        return IResponse.success(workflowService.queryAllNode());
    }

    @GetMapping("/pageQueryNode/{pageSize}/{pageIndex}")
    public IResponse pageQueryNode(@PathVariable long pageSize, @PathVariable long pageIndex) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageIndex);
        return IResponse.success(workflowService.pageQueryNode(page));
    }

    @GetMapping("/pageQueryNodeUser/{pageSize}/{pageIndex}/{nodeId}")
    public IResponse pageQueryNodeUser(@PathVariable long pageSize, @PathVariable long pageIndex, @PathVariable String nodeId) {
        return IResponse.success(workflowService.queryNodeUser(nodeId, pageIndex, pageSize));
    }

    @GetMapping("/addUser/{userName}/{userId}/{nodeId}")
    public IResponse addUser(@PathVariable String userName, @PathVariable String userId, @PathVariable String nodeId) {
        UserVo userVo = new UserVo();
        userVo.setUsername(userId);
        userVo.setUserRealName(userName);
        return IResponse.success(workflowService.addNodeUser(nodeId, Arrays.asList(userVo)));
    }

    @GetMapping("/changeUserTotalWeifht/{userName}/{weight}")
    public IResponse changeUserTotalWeifht(@PathVariable String userName, @PathVariable int weight) {
        return IResponse.success(workflowService.changeTotalWeight(userName, weight));
    }

    @GetMapping("/changeUserWeight/{userIds}/{weight}")
    public IResponse changeUserWeight(@PathVariable String userIds, @PathVariable int weight) {
        return IResponse.success(workflowService.changeWeight(Arrays.asList(userIds.split(",")), weight));
    }

    @GetMapping("/clearTaskCount/{userIds}")
    public IResponse clearTaskCount(@PathVariable String userIds) {
        return IResponse.success(workflowService.clearTaskCount(Arrays.asList(userIds.split(","))));
    }

    @GetMapping("/removeNodeUsers/{userIds}")
    public IResponse removeNodeUsers(@PathVariable String userIds) {
        return IResponse.success(workflowService.removeNodeUsers(Arrays.asList(userIds.split(","))));
    }

    @GetMapping("/moveUserToNode")
    public IResponse removeNodeUsers(@RequestParam("operId") String operId, @RequestParam("fromTaskId") String fromTaskId, @RequestParam("toTaskId") String toTaskId) {
        return IResponse.success(workflowService.moveUserToNode(operId, fromTaskId, toTaskId));
    }

    @GetMapping("/start")
    public IResponse startWorkFlow() {
        StartFlowRequest startFlowRequest = new StartFlowRequest();
        startFlowRequest.setAfsFlowKey("test-design");
        startFlowRequest.setBizDataId(uidGenerator.getUID() + "");
        startFlowRequest.setFlowName("测试流程");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("test", new JSONObject());
        jsonObject.getJSONObject("test").put("testselect", "0");
        jsonObject.getJSONObject("test").put("testdic", "2");
        jsonObject.getJSONObject("test").put("cascade", "2");
        return IResponse.success(workflowService.startWorkFlow(startFlowRequest, jsonObject));
    }

    @GetMapping("/queryCmd/{flowId}")
    public IResponse queryCmd(@PathVariable String flowId) {
        QueryFLowCmdRequest queryFLowCmdRequest = new QueryFLowCmdRequest();
        queryFLowCmdRequest.setFlowInstanceId(flowId);
        return IResponse.success(workflowService.queryFlowCmd(queryFLowCmdRequest));
    }

    @GetMapping("/queryInstance/{flowId}")
    public IResponse queryInstance(@PathVariable String flowId) {
        QueryFlowInstanceRequest queryFLowCmdRequest = new QueryFlowInstanceRequest();
        queryFLowCmdRequest.setFlowInstanceId(flowId);
        return IResponse.success(workflowService.queryFlowInstance(queryFLowCmdRequest));
    }

    @GetMapping("/flowGiveup/{flowId}")
    public IResponse flowGiveup(@PathVariable String flowId) {
        FLowGiveUpRequest fLowGiveUpRequest = new FLowGiveUpRequest();
        fLowGiveUpRequest.setFlowInstanceId(flowId);
        fLowGiveUpRequest.setReason("废弃");
        return IResponse.success(workflowService.flowGiveUp(fLowGiveUpRequest));
    }


    @GetMapping("/taskSubmit")
    public IResponse taskSubmit(@RequestParam String dataId, @RequestParam String gwtUserId, @RequestParam String cmdId, @RequestParam String cmdRemark, @RequestParam(required = false) String nextUserid) {
        TaskSubmitRequest taskSubmitRequest = new TaskSubmitRequest();
        taskSubmitRequest.setGwtUserId(gwtUserId);
        taskSubmitRequest.setAfsFlowKey("test-design");
        taskSubmitRequest.setBizDataId(dataId);
        ;
        taskSubmitRequest.setCmdId(cmdId);
        taskSubmitRequest.setRemark(cmdRemark);
        if (!StringUtils.isEmpty(nextUserid)) {
            taskSubmitRequest.setNextUserName(nextUserid);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("test", new JSONObject());
        jsonObject.getJSONObject("test").put("testselect", "0");
        jsonObject.getJSONObject("test").put("testdic", "0");
        jsonObject.getJSONObject("test").put("cascade", "2");
        return IResponse.success(workflowService.taskSubmit(taskSubmitRequest, jsonObject));
    }


    @GetMapping("/taskReformist")
    public IResponse taskReformist(@RequestParam("flowInstanceId") String flowInstanceId, @RequestParam("userName") String userName, @RequestParam("userRealName") String userRealName) {
        TaskReformistRequest taskReformistRequest = new TaskReformistRequest();
        taskReformistRequest.setFlowInstanceId(flowInstanceId);
        taskReformistRequest.setUserName(userName);
        taskReformistRequest.setAfsFlowKey("test-design");
        taskReformistRequest.setBizDataId("4492892720581902336");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("test", new JSONObject());
        jsonObject.getJSONObject("test").put("testselect", "0");
        jsonObject.getJSONObject("test").put("testdic", "0");
        jsonObject.getJSONObject("test").put("cascade", "2");
        return IResponse.success(workflowService.taskReformist(taskReformistRequest, jsonObject));
    }


    @GetMapping("/taskCallBack")
    public IResponse taskCallBack(@RequestParam("flowInstanceId") String flowInstanceId, @RequestParam("userName") String userName, @RequestParam("userRealName") String userRealName) {
        TaskCallBackRequest taskReformistRequest = new TaskCallBackRequest();
        taskReformistRequest.setFlowInstanceId(flowInstanceId);
        taskReformistRequest.setUserName(userName);
        taskReformistRequest.setAfsFlowKey("test-design");
        taskReformistRequest.setBizDataId("4492892720581902336");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("test", new JSONObject());
        jsonObject.getJSONObject("test").put("testselect", "0");
        jsonObject.getJSONObject("test").put("testdic", "0");
        jsonObject.getJSONObject("test").put("cascade", "2");
        return IResponse.success(workflowService.taskCallBack(taskReformistRequest, jsonObject));
    }

    @GetMapping("/getcascader")
    public IResponse<List<GwtCascadeDto>> cascadeDtoIResponse() {
        List<GwtCascadeDto> gwtCascadeDtos = new ArrayList<>();
        GwtCascadeDto gwtCascadeDto = new GwtCascadeDto();
        gwtCascadeDto.setLabel("李远康");
        gwtCascadeDto.setValue("lyk");
        gwtCascadeDto.setChildren(new ArrayList<>());
        GwtCascadeDto gwtCascadeDto1 = new GwtCascadeDto();
        gwtCascadeDto1.setLabel("李远康儿子");
        gwtCascadeDto1.setValue("lykez");
        gwtCascadeDto1.setChildren(new ArrayList<>());
        GwtCascadeDto gwtCascadeDtosi = new GwtCascadeDto();
        gwtCascadeDtosi.setLabel("李远康孙子");
        gwtCascadeDtosi.setValue("lyksz");
        gwtCascadeDto1.getChildren().add(gwtCascadeDtosi);
        GwtCascadeDto gwtCascadeDto2 = new GwtCascadeDto();
        gwtCascadeDto2.setLabel("李远康女儿");
        gwtCascadeDto2.setValue("lykner");

        gwtCascadeDto.getChildren().add(gwtCascadeDto1);
        gwtCascadeDto.getChildren().add(gwtCascadeDto2);


        GwtCascadeDto gwtCascadeliYang = new GwtCascadeDto();
        gwtCascadeliYang.setValue("test");
        gwtCascadeliYang.setLabel("测试测试");
        gwtCascadeDtos.add(gwtCascadeDto);
        gwtCascadeDtos.add(gwtCascadeliYang);
        return IResponse.success(gwtCascadeDtos);
    }


    @GetMapping("/resumeFlowInstance")
    public IResponse<List<GwtCascadeDto>> resumeFlowInstance(@RequestParam("exceptionId") String exceptionId, @RequestParam("bizDataId") String bizDataId) {
        return IResponse.success(workflowService.resumeFlow(bizDataId, exceptionId));
    }

    @GetMapping("/pauseFlowInstance")
    public IResponse<List<GwtCascadeDto>> pauseFlowInstance(@RequestParam("flowInstanceId") String flowInstanceId) {
        return IResponse.success(workflowService.pauseFlowInstance(flowInstanceId));
    }
}
